package OptimalAlgorithm.Hash;

import java.util.HashMap;

//https://leetcode.cn/problems/contains-duplicate-ii/description/
public class ContainsNearbyDuplicate {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        HashMap<Integer,Integer> hash = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(hash.containsKey(nums[i]) && Math.abs(hash.get(nums[i]) - i) <= k){
                return true;
            }
            hash.put(nums[i],i);
        }
        return false;
    }
}
